class Solution:
    def findFarmland(self, land: list[list[int]]):  # -> list[list[int]]:
        # Barier method
        land_width = len(land[0])
        land_height = len(land)

        land = [[0] + row + [0] for row in land]

        land.insert(0, [0] * (land_width + 2))
        land.append([0] * (land_width + 2))

        coordinates = []
        result = []

        def dfs_mark(i, j):
            if land[i][j] != 1:
                return
            land[i][j] = -1
            dfs_mark(i, j+1)
            dfs_mark(i+1, j)

        def get_bottom(i, j):
            while land[i][j] == -1:
                i += 1
            return i-2

        def get_right(i, j):
            while land[i][j] == -1:
                j += 1
            return j-2

        for i in range(1, land_height + 1):
            for j in range(1, land_width + 1):
                if land[i][j] == 1:
                    coordinates.extend([i-1, j-1])
                    dfs_mark(i, j)
                    coordinates.extend([get_bottom(i, j), get_right(i, j)])
                    result.append(coordinates)
                    coordinates = []

        return result
